package com.mr.pay.service;

import com.mr.base.domain.Card;
import com.mr.pay.dao.CustDao;
import com.mr.pay.domain.Cust;
import com.mr.pay.domain.CustLine;
import com.skysz.framework.action.ActionUtils;
import com.skysz.framework.dao.DomainDao;
import com.skysz.framework.domain.Entity;
import com.skysz.framework.service.AbstractBaseService;
import com.skysz.framework.utils.Result;

/**
 * 客户管理
 * @author zhaow
 */
public class CustService<T extends Entity> extends AbstractBaseService<Cust> {
	private static final long serialVersionUID = 1L;
	
	private CustDao<Cust> custDao;
	
	
	@Override
	public DomainDao<Cust> getDomainDao() {
		return custDao ;
	}
	
	public void setCustDao(CustDao<Cust> custDao) {
		this.custDao = custDao;
	}




	/**
	 * 新增客户
	 * @param cust
	 * @return
	 */
	public Result insertCust(Cust cust) {
		Result r = new Result();
		r.setCode(Result.CODE_FAILURE);
		
		//判断手机号码唯一性
		Cust ct = new Cust();
		ct.setPhone(cust.getPhone());
		ct.setEnable(true);
		if (!this.isUnique(ct)) {
			r.setInfo("手机号码已被注册。");
			return r;
		}
		
		this.insert(cust);
		
		//新增客户卡
		Card card = cust.getCard();
		if (card != null) {
			CustLine cl = new CustLine();
			cl.setCard(card);
			cl.setOrg(ActionUtils.getCurrentOrg()) ;
			ActionUtils.setWho(cl);
			
			this.utilsDao.insert(cl);
		}
		
		r.setCode(Result.CODE_SUCCESS);
		return r;
	}

}
